package com.tanjun.mallservice.web.biz.impl;

import com.tanjun.common.model.LoginUserInfo;
import com.tanjun.common.utils.ConvertUntil;
import com.tanjun.mallservice.apiservice.request.shopcar.DelShopCarVo;
import com.tanjun.mallservice.apiservice.request.shopcar.SetShopCarVo;
import com.tanjun.mallservice.apiservice.response.plu.MallPluDto;
import com.tanjun.mallservice.apiservice.response.plu.MallPluSkuDto;
import com.tanjun.mallservice.apiservice.response.shopcar.MallShopCarDto;
import com.tanjun.mallservice.apiservice.response.ven.VenDto;
import com.tanjun.mallservice.web.biz.MallShopCarBiz;
import com.tanjun.mallservice.web.orm.entity.MallPlu;
import com.tanjun.mallservice.web.orm.entity.MallPluSku;
import com.tanjun.mallservice.web.orm.entity.MallShopCar;
import com.tanjun.mallservice.web.orm.entity.MallVen;
import com.tanjun.mallservice.web.service.MallPluService;
import com.tanjun.mallservice.web.service.MallPluSkuService;
import com.tanjun.mallservice.web.service.MallShopCarService;
import com.tanjun.mallservice.web.service.MallVenService;
import com.tanjun.web.service.LoginUserUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MallShopCarBizImpl implements MallShopCarBiz {
    @Autowired
    private MallShopCarService mallShopCarService;
    @Autowired
    private MallPluService mallPluService;
    @Autowired
    private MallPluSkuService mallPluSkuService;
    @Autowired
    private MallVenService mallVenService;

    /**
     * 修改购物车商品数量
     *
     * @param paramVo
     */
    @Override
    public void changeShopCarNum(SetShopCarVo paramVo) {
        LoginUserInfo userInfo = LoginUserUtil.getLoginUserInfo();
        mallShopCarService.setShopCarNum(userInfo.getUserId(),
                paramVo.getPluId(), paramVo.getSkuId(),
                paramVo.getSkuNum(),
                paramVo.getIsAdd()
        );
    }

    /**
     * 删除购物车中物资
     *
     * @param paramVo
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteShopCar(DelShopCarVo paramVo) {
        LoginUserInfo userInfo = LoginUserUtil.getLoginUserInfo();
        mallShopCarService.deleteShopCar(userInfo.getUserId(), paramVo.getShopCarIdList());
    }

    /**
     * 获取用户购物车详细列表
     * @return
     */
    @Override
    public List<MallShopCarDto> getListShopCarDetail() {
        LoginUserInfo userInfo = LoginUserUtil.getLoginUserInfo();
        List<MallShopCar> listShopCarPluSku= mallShopCarService.listShopCarPluSku(userInfo.getUserId());
        if(CollectionUtils.isEmpty(listShopCarPluSku)){
            return Collections.emptyList();
        }
        List<MallPlu> listPlu= mallPluService.listPluByIds(listShopCarPluSku.stream().map(MallShopCar::getPluId).collect(Collectors.toSet()));
        List<MallPluSku> listSku=mallPluSkuService.listSkuByIds(listShopCarPluSku.stream().map(MallShopCar::getSkuId).collect(Collectors.toSet()));
        Map<Long,MallPlu> mapPlu=listPlu.stream().collect(Collectors.toMap(MallPlu::getPluId, Function.identity()));
        Map<Long,MallPluSku> mapSku=listSku.stream().collect(Collectors.toMap(MallPluSku::getSkuId,Function.identity()));

        if(CollectionUtils.isEmpty(mapPlu)||CollectionUtils.isEmpty(mapSku)){
            return Collections.emptyList();
        }

        List<MallVen> listVen=mallVenService.listVenByIds(listPlu.stream().map(MallPlu::getVenId).collect(Collectors.toSet()));
        Map<Long,MallVen> mapVen=listVen.stream().collect(Collectors.toMap(MallVen::getVenId,Function.identity()));

        List<MallShopCarDto> list=new LinkedList<>();
        listShopCarPluSku.forEach(mallShopCar -> {
            MallPlu plu=mapPlu.get(mallShopCar.getPluId());
            MallPluSku sku=mapSku.get(mallShopCar.getSkuId());
            MallVen ven=mapVen.get(plu!=null?plu.getVenId():-1);
            if (plu==null||sku==null||ven==null){
                log.error("购物车存在异常数据,shopCardId:{},pluId:{},skuId:{}",mallShopCar.getShopCarId(),
                        mallShopCar.getPluId(),mallShopCar.getSkuId());
            }
            else {
                MallShopCarDto shopCarDto=list.stream().filter(t-> Objects.equals(t.getVen().getVenId(), ven.getVenId()))
                        .findFirst().orElse(null);
                if(shopCarDto==null){
                    shopCarDto=new MallShopCarDto();
                    list.add(shopCarDto);
                }
                if(shopCarDto.getVen()==null){
                    shopCarDto.setVen(ConvertUntil.convert(ven, VenDto.class));
                }
                if(CollectionUtils.isEmpty(shopCarDto.getListShopCardSku())){
                    shopCarDto.setListShopCardSku(new LinkedList<>());
                }
                MallShopCarDto.MallShopCardSkuInfo mallShopCardSkuInfo=new MallShopCarDto.MallShopCardSkuInfo();
                mallShopCardSkuInfo.setPlu(ConvertUntil.convert(plu, MallPluDto.class));
                mallShopCardSkuInfo.setSku(ConvertUntil.convert(sku, MallPluSkuDto.class));
                mallShopCardSkuInfo.setSkuNum(mallShopCar.getSkuNum());
                mallShopCardSkuInfo.setAddSkuPrice(mallShopCar.getAddSkuPrice());
                mallShopCardSkuInfo.setShopCarId(mallShopCar.getShopCarId());

                shopCarDto.getListShopCardSku().add(mallShopCardSkuInfo);

            }

        });

        return list;
    }

    @Override
    public Integer countShopCarSkuNum() {
        LoginUserInfo userInfo = LoginUserUtil.getLoginUserInfo();
        return (int) mallShopCarService.countShopCarPluSku(userInfo.getUserId());
    }
}
